package com.zhou.common.base.mq.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.stereotype.Component;

import java.util.Locale;

@Slf4j
@Component
public class I18nUtil {

    private static ResourceBundleMessageSource messageSource;

    public static String getMessage(String code, Object[] args, String defaultMessage, Locale locale) {
        String content = null;
        try {
            content = messageSource.getMessage(code, args, defaultMessage, locale);
        } catch (Exception e) {
            log.error("国际化参数获取失败===>{},{}", e.getMessage(), e);
            content = defaultMessage;
        }
        return content;
    }

    public static String getMessage(String code, Object[] args, Locale locale) {
        String content = null;
        try {
            content = messageSource.getMessage(code, args, locale);
        } catch (Exception e) {
            log.error("国际化参数获取失败===>{},{}", e.getMessage(), e);
        }
        return content;
    }

    @Autowired(required = false)
    public void setMessageSource(ResourceBundleMessageSource messageSource) {
        I18nUtil.messageSource = messageSource;
    }


}

